/*
 * Created on 21/04/2008
 * 
 */
package com.apbetioli.mapr.place.control.kml;

import com.apbetioli.mapr.place.dao.PlacesDao;
import com.apbetioli.mapr.place.entity.Place;
import com.apbetioli.mapr.place.exception.NullGeoPointException;
import com.apbetioli.kml.schema.CtDocument;
import com.apbetioli.kml.schema.CtIcon;
import com.apbetioli.kml.schema.CtIconStyle;
import com.apbetioli.kml.schema.CtPlacemark;
import com.apbetioli.kml.schema.CtStyle;
import com.apbetioli.kml.schema.Kml;
import com.apbetioli.kml.schema.ObjectFactory;
import com.apbetioli.mapr.util.properties.PropertiesLoaderImpl;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

/**
 *
 * @author Alexandre Parra Betioli
 */
public class KMLGenerator {

    private static final String KML_FOLDER   = PropertiesLoaderImpl.getValue("config.properties", "kml.generator.folder");
    private static final String KML_DIR     = System.getProperty("user.home") + File.separator + KML_FOLDER;
    private static final String SCHEMA_PATH  = PropertiesLoaderImpl.getValue("config.properties", "kml.generator.schema");
    private static final String ICON_PLANTED = PropertiesLoaderImpl.getValue("config.properties", "kml.generator.icon.planted");
    private static final String ICON_FUTURE  = PropertiesLoaderImpl.getValue("config.properties", "kml.generator.icon.future");
        
    public  static final String STYLE_ID_PLANTED = PropertiesLoaderImpl.getValue("config.properties", "kml.generator.styleid.planted");
    public  static final String STYLE_ID_FUTURE  = PropertiesLoaderImpl.getValue("config.properties", "kml.generator.styleid.future");
    
    private JAXBContext jc;
    private ObjectFactory of;
    
    
    public void generate( final List<Place> places, final String projectKey) throws JAXBException, FileNotFoundException {
        jc = JAXBContext.newInstance( SCHEMA_PATH );
        of = new ObjectFactory();
        
        PlacesDao dao = new PlacesDao();
        
        Kml kml = create( places );
        marshall( kml, projectKey );
    }

    public boolean delete(String projectKey) {
        File file = new File(KML_DIR + projectKey + ".kml");
        return file.delete();
    }
    
    private Kml create( final List<Place> places ) throws JAXBException, FileNotFoundException {
        
        Kml         kml = of.createKml();
        CtDocument  doc = of.createCtDocument();
        
        CtStyle     styPlanted = of.createCtStyle();
        CtIconStyle icsPlanted = of.createCtIconStyle();
        CtIcon      icoPlanted = of.createCtIcon();
        
        CtStyle     styFuture  = of.createCtStyle();
        CtIconStyle icsFuture  = of.createCtIconStyle();
        CtIcon      icoFuture  = of.createCtIcon();

        //Document
        
        kml.setDocument(doc);
        
        //Styles
        
        doc.getStyle().add(styPlanted);
        styPlanted.setId(STYLE_ID_PLANTED);
        styPlanted.setIconStyle(icsPlanted);
        icsPlanted.setIcon(icoPlanted);
        icoPlanted.setHref(ICON_PLANTED);
        
        doc.getStyle().add(styFuture);
        styFuture.setId(STYLE_ID_FUTURE);
        styFuture.setIconStyle(icsFuture);
        icsFuture.setIcon(icoFuture);
        icoFuture.setHref(ICON_FUTURE);

        //Placemarks
        
        for( Place place : places ) {
            try {
                CtPlacemark placemark = place.toPlacemark();
                doc.getPlacemark().add(placemark);
            } catch (NullGeoPointException ex) {
                Logger.getLogger(KMLGenerator.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        
        return kml;
        
    }

    private void marshall( final Kml kml, final String projectKey) throws JAXBException, FileNotFoundException {
        
        File dir = new File( KML_DIR );
        if (! dir.exists() )
            dir.mkdirs();
        
        Marshaller marshal = jc.createMarshaller();
        File       file    = new File(KML_DIR + projectKey + ".kml");
        marshal.marshal(kml, new FileOutputStream(file));
    }
    
}
